﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    Layout = null;
}

<link href="~/lib/layui/css/layui.css" rel="stylesheet" />
<script src="~/lib/layui/layui.js"></script>
<script src="~/js/js.cookie.js"></script>
<script src="~/js/moment.js"></script>
<script src="~/lib/jquery/dist/jquery.js"></script>

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Quick Start - Layui</title>
    <!-- 引入 layui.css margin-right:-100px" -->
    <link href="//unpkg.com/layui@2.9.20/dist/css/layui.css" rel="stylesheet">
</head>
<body>
    <div class="layui-bg-gray" style="padding: 16px;" id="Inquire">
        <div class="layui-panel" style="width:100%;">
            <div class="layui-card">
                <div class="layui-card-header">
                    查询条件
                </div>
                @* 主体 *@
                <div class="layui-card-body">
                    <form class="layui-form layui-row layui-col-space16" lay-filter="demo-val-filter" action="">
                        <div class="layui-col-md4">
                            <div class="layui-form-item" style="margin-bottom: 10px;">
                                <label class="layui-form-label">订单编码</label>
                                <div class="layui-input-block">
                                    <input type="text" name="purchaseNumber" placeholder="请输入" class="layui-input">
                                </div>
                            </div>
                        </div>

                        <div class="layui-col-md4">
                            <div class="layui-form-item" style="margin-bottom: 10px;">
                                <label class="layui-form-label">订单名称</label>
                                <div class="layui-input-block">
                                    <input type="text" name="purchaseName" placeholder="请输入" class="layui-input">
                                </div>
                            </div>
                        </div>

                        <div class="layui-col-md4">
                            <div class="layui-form-item" style="margin-bottom: 10px;">
                                <label class="layui-form-label">订单类型</label>
                                <div class="layui-input-block">
                                    <select name="purchaseTypeId"></select>
                                </div>
                            </div>
                        </div>

                        <div class="layui-col-md4">
                            <div class="layui-form-item" style="margin-bottom: 10px;">
                                <label class="layui-form-label">供应商</label>
                                <div class="layui-input-block">
                                    <select name="supplierId"></select>
                                </div>
                            </div>
                        </div>

                        <div class="layui-col-md4">
                            <div class="layui-form-item" style="margin-bottom: 10px;">
                                <label class="layui-form-label">项目</label>
                                <div class="layui-input-block">
                                    <select name="affiliatedProjectId"></select>
                                </div>
                            </div>
                        </div>

                        <div class="layui-col-md4">
                            <div class="layui-form-item" style="margin-bottom: 10px;">
                                <label class="layui-form-label">状态</label>
                                <div class="layui-input-block">
                                    <select name="purchaseStatus">
                                        <option value="0">请选择</option>
                                        <option value="1">新增</option>
                                        <option value="2">变更</option>
                                        <option value="3">确认</option>
                                        <option value="4">取消</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-md6"></div>
                        <div class="layui-col-md6">
                            <div class="layui-form-item" style="float:right;width:350px">
                                <div class="layui-input-block">
                                    <button type="reset" class="layui-btn layui-btn-primary" style="float:left">重置</button>
                                    <div style="width: 150px; float:right;margin-left:10px">
                                        <button type="submit" id="LookInquire" class="layui-btn layui-bg-blue layui-btn-fluid" lay-submit>查询</button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <div class="layui-bg-gray" style="padding: 16px;">
        <div class="layui-panel">
            <div class="layui-card">
                <div class="layui-card-header">
                    <span style="font:bolder">采购/委外订单列表</span>
                    <div style="float:right;">
                        <button type="button" style="margin-right:10px;" class="layui-btn layui-btn-sm layui-icon layui-icon-addition" onclick="location.href='/PurchaseOutsource/CreatePurchaseOutsource'">新增采购订单</button>
                        <button type="button" class="layui-btn layui-btn-primary layui-btn-sm layui-border layui-icon layui-icon-search" id="Unfold">展开查询</button>
                        <button type="button" class="layui-btn layui-btn-primary layui-btn-sm layui-border layui-icon layui-icon-search" id="Close">关闭查询</button>
                        <button type="button" class="layui-btn layui-btn-primary layui-btn-sm layui-border layui-icon layui-icon-export" id="Export" onclick="ExportPurchase()">导出</button>
                        
                    </div>

                </div>
                <div class="layui-card-body">
                    @* 表格 *@
                    <table class="layui-table">
                        <thead>
                            <tr>
                                <th>序号</th>
                                <th>订单编码</th>
                                <th>订单名称</th>
                                <th>订单类型</th>
                                <th>所属项目</th>
                                <th>供应商</th>
                                <th>签订日期</th>
                                <th>制单人</th>
                                <th>制单部门</th>
                                <th>创建时间</th>
                                <th>状态</th>
                                <th>审批状态</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody></tbody>
                    </table>
                    <div id="demo-laypage-all"></div>
                </div>
            </div>
        </div>
    </div>


    <script>
        // 使用layui的layer模块
        layui.use(['form', 'table', 'layer'], function () {
            var $ = layui.$;
            var form = layui.form;
            var table = layui.table;
            var laypage = layui.laypage;
            var upload = layui.upload;

            //获取分页数据
            var pageIndex = 1;
            var pageSize = 3;

            $(function () {
                //打开页面隐藏查询条件
                $('#Inquire').toggle();
                //隐藏关闭查询
                $('#Close').hide();
                SearchPurchaseOutsource(pageIndex, pageSize);//采购/委外列表
            })


            // 展开查询
            $('#Unfold').on('click', function () {
                $('#Inquire').show();
                //显示后 隐藏展开查询
                $('#Unfold').hide();
                //显示关闭查询
                $('#Close').show();
            });

            // 关闭查询
            $('#Close').on('click', function () {
                $('#Inquire').hide();
                //关闭后 隐藏关闭查询
                $('#Close').hide();
                //关闭后 显示展开查询
                $('#Unfold').show();
            });

            //获取订单类型
            $.ajax({
                url: '@ViewBag.RedDragonWrite' + "/api/Purchase/SearchPurchaseType",
                type: 'GET',
                dataType: 'json',
                success: res => {
                    console.log(res)
                    var options = '<option value="0">请选择</option>';
                    $.each(res.data, function (index, item) {
                        options += '<option value="' + item.purchaseTypeId + '">' + item.purchaseTypeName + '</option>';
                    });
                    $('[name="purchaseTypeId"]').html(options);
                    form.render('select'); // 更新下拉框渲染
                }
            });

            //获取所属项目
            $.ajax({
                url: '@ViewBag.RedDragonWrite' + "/api/Purchase/SearchAffiliatedProject",
                type: 'GET',
                dataType: 'json',
                success: res => {
                    console.log(res)
                    var options = '<option value="0">请选择</option>';
                    $.each(res.data, function (index, item) {
                        options += '<option value="' + item.affiliatedProjectId + '">' + item.affiliatedProjectName + '</option>';
                    });
                    $('[name="affiliatedProjectId"]').html(options);
                    form.render('select'); // 更新下拉框渲染
                }
            });

            //获取供应商
            $.ajax({
                url: '@ViewBag.RedDragonWrite' + "/api/Purchase/SearchAllSupplier",
                type: 'GET',
                dataType: 'json',
                success: res => {
                    console.log(res)
                    var options = '<option value="0">请选择</option>';
                    $.each(res.data, function (index, item) {
                        options += '<option value="' + item.supplierId + '">' + item.supplierName + '</option>';
                    });
                    $('[name="supplierId"]').html(options);
                    form.render('select'); // 更新下拉框渲染
                }
            });

            // 绑定查询按钮点击事件
            $('#LookInquire').on('click', function () {
                SearchPurchaseOutsource(pageIndex, pageSize);
                return false; // 阻止默认 form 跳转
            });

            //获取采购/委外订单列表
            function SearchPurchaseOutsource(index, size) {
                //获取数据
                var data = {
                    pageIndex: index,
                    pageSize: size,
                    purchaseNumber: $("[name='purchaseNumber']").val(),
                    purchaseName: $("[name='purchaseName']").val(),
                    purchaseTypeId: $("[name='purchaseTypeId']").val() || 0,
                    supplierId: $("[name='supplierId']").val() || 0,
                    affiliatedProjectId: $("[name='affiliatedProjectId']").val() || 0,
                    purchaseStatus: $("[name='purchaseStatus']").val(),
                }
                console.log(data);
                var totalCount = 0;//总条数
                var totalPage = 0;//总页数
                //获取
                $.ajax({
                    url: '@ViewBag.RedDragonWrite' + "/api/Purchase/SearchPurchaseOutsource",
                    type: 'post',
                    data: JSON.stringify(data),
                    dataType: 'json',
                    contentType: "application/json",
                    success: res => {
                        console.log("委外订单", res);
                        $("tbody").empty();
                        if (res.data != null) {
                            totalCount = res.data.totalCount;
                            totalPage = res.data.pageCount;
                            $.each(res.data.list, function (index, item) {
                                if (item.createdTime == null) {
                                    item.createdTime = "";
                                }
                                else {
                                    item.createdTime = moment(item.createdTime).format("YYYY-MM-DD HH:mm:ss")
                                }
                                if (item.theDateOfSigning == null) {
                                    item.theDateOfSigning = "";
                                }
                                else {
                                    item.theDateOfSigning = moment(item.theDateOfSigning).format("YYYY-MM-DD HH:mm:ss")
                                }
                                $("tbody").append(`<tr>` +
                                    `<td>${item.purchaseId}</td>` +
                                    `<td>${item.purchaseNumber}</td>` +
                                    `<td>${item.purchaseName}</td>` +
                                    `<td>${item.purchaseTypeName}</td>` +
                                    `<td>${item.affiliatedProjectName}</td>` +
                                    `<td>${item.supplierName}</td>` +
                                    `<td>${item.theDateOfSigning}</td>` +
                                    `<td>${item.preparedBy}</td>` +
                                    `<td>${item.departmentName}</td>` +
                                    `<td>${item.createdTime}</td>` +
                                    `<td>${item.purchaseStatus == 1 ? "新建" : item.purchaseStatus == 2 ? "变更" : item.purchaseStatus == 3 ? "确认" : "取消"}</td > ` +
                                    `<td>${item.currencyName}</td>` +
                                    `<td><button type="button" class="layui-btn layui-btn-primary layui-btn-xs layui-border layui-icon layui-icon-edit" onclick="UpdatePurchase('${item.purchaseNumber}')">编辑</button></td>` +
                                    `</tr>`)
                            });
                        }
                        laypage.render({
                            elem: 'demo-laypage-all',
                            curr: data.pageIndex,//当前页
                            limit: data.pageSize,//每页显示条数
                            limits: [3, 6, 9, 12],//每页条数的选择项
                            count: totalCount, // 数据总数，从后端得到
                            first: "首页",
                            prev: "上一页",
                            next: "下一页",
                            last: "尾页",
                            layout: ['count', 'first', 'prev', 'page', 'next', 'last', 'limit', 'skip'], // 自定义布局
                            jump: function (obj, first) {
                                console.log("分页", obj);
                                // 首次不执行
                                if (!first) {
                                    pageIndex = obj.curr;
                                    pageSize = obj.limit;
                                    SearchPurchaseOutsource(pageIndex, pageSize);
                                }
                            }
                        });
                    }
                });
            }
        });

        //编辑/采购委外订单
        function UpdatePurchase(purchaseNumber) {
            location.href = "/PurchaseOutsource/UpdatePurchaseOutsource?PurchaseNumber=" + purchaseNumber;
        }

        //导出采购委外订单
        function ExportPurchase() {
            location.href = '@ViewBag.RedDragonWrite' + "/api/Purchase/ExportPurchase";
        }


    </script>
</body>
</html>

